/*
 * @Author: your name
 * @Date: 2021-01-28 09:17:23
 * @LastEditTime: 2021-04-16 11:53:47
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tower_defense\assets\scripts\game_scene\tower_builder.ts
 */

import { UG } from "../modules/ugame"
import { ArrowTowerParams } from "../game_data/tower_data/arrow_tower_params"
import { CannonTowerParams } from "../game_data/tower_data/cannon_tower_params"
import { ElectricityTowerParams } from "../game_data/tower_data/electricity_tower_params"
import { WarlockTowerParams } from "../game_data/tower_data/warlock_tower_params"

const { ccclass, property } = cc._decorator;

@ccclass
export default class TowerBuilder extends cc.Component {

    @property(cc.Prefab)    // 塔预制体
    public tower_prefabs_arr: Array<cc.Prefab> = [];

    public tower_params = [];   // 每个塔的参数数据
    public tower_type: any = null;   // 塔类型
    public tower_sprite_node: cc.Node = null;   // 塔精灵节点
    public builded_tower: cc.Node = null;   // 建造的塔
    public tower_root: cc.Node = null;  // 存放塔的节点
    public tower_menu = null;       // 建筑菜单组件
    public is_builded: boolean = false; // 标注这个位置已经建塔
    public game_scene = null;   // 组件 

    onLoad() {
        // 塔参数
        this.tower_params = [null, null, null, null];
        this.tower_params[0] = ArrowTowerParams;    // 弓箭
        this.tower_params[1] = ElectricityTowerParams;  // 电塔
        this.tower_params[2] = CannonTowerParams;   // 炮塔
        this.tower_params[3] = WarlockTowerParams; // 法师塔

        this.tower_sprite_node = this.node.getChildByName('tower_sprite');  // 塔精灵节点
        this.tower_root = cc.find("Canvas/tower_panel/tower_root");  // 塔放置节点
        this.tower_menu = cc.find("Canvas/tower_menu").getComponent("tower_menu");
        this.game_scene = cc.find("Canvas/game_scene").getComponent("game_scene");

    }

    // 1 弓箭 2 电塔 3 炮塔 4 法师塔
    public on_tower_build_click(e, tower_type: number): void {
        if (tower_type < 0 || tower_type >= 4) {
            return;
        }
        // 检查金币是否足够
        if (!this.check_uchip_when_build(tower_type, 1)) {
            return;
        }
        this.tower_type = tower_type.toString();

        // 把待建精灵节点隐藏
        this.tower_sprite_node.active = false;

        // 建塔
        let center_pos: cc.Vec2 = this.node.getPosition();
        this.builded_tower = cc.instantiate(this.tower_prefabs_arr[tower_type]);
        this.builded_tower.setPosition(center_pos);
        this.builded_tower.parent = this.tower_root;
        this.builded_tower.active = true;
        this.is_builded = true;

        // 消耗金币
        const build_chip: number = this.tower_params[tower_type][0] .build_chip;
        UG.add_chip(-build_chip);
        this.game_scene.show_game_uchip();
    }

    // 检查金钱是否足够建塔
    public check_uchip_when_build(tower_type: number, level: number): boolean {
        const build_chip: number = this.tower_params[tower_type][level - 1].build_chip;
        const uchip: number = UG.get_uchip();

        if (uchip >= build_chip) {
            return true;
        }
        return false;
    }

    // 点击事件 --> 待建位置显示菜单界面
    public show_tower_menu_panel(e): void {
        if (this.is_builded === false) {    // 还没建塔,显示建塔界面
            this.tower_menu.show_tower_build_panel(this);
        } else {    // 已经建塔,显示升级和撤销界面
            this.tower_menu.show_tower_upgrade_panel(this);
        }
        this.tower_menu.node.scale = 0;
        cc.tween(this.tower_menu.node)
            .to(0.3, { scale: 1 }, { easing: 'backOut' })
            .start();
    }

    // 获取节点上的防御塔组件
    public get_tower_com(): any {
        let tower_com: any = null;
        // 1 弓箭 2 电塔 3 炮塔 4 法师塔
        switch (this.tower_type) {
            case "0": // 弓箭
                tower_com = this.builded_tower.getComponent("arrow_tower");
                break;
            case "1": // 电塔
                tower_com = this.builded_tower.getComponent("electricity_tower");
                break;
            case "2": // 炮塔
                tower_com = this.builded_tower.getComponent("cannon_tower");
                break;
            case "3": // 法师塔
                tower_com = this.builded_tower.getComponent("warlock_tower");
                break;
            default:
                break;
        }
        return tower_com;
    }

    // 塔升级
    public on_tower_upgrade_click(): void {
        // 判断是否已经有塔
        if (this.is_builded === false || this.builded_tower === null) {
            cc.log('塔不存在');
            return;
        }

        // 塔组件
        let tower_com: any = this.get_tower_com();
        if (!tower_com) {
            cc.log('塔组件为null');
            return;
        }

        // 判断升级所需金币是否足够
        let tower_level: any = tower_com.get_tower_level();
        if (tower_level === true) {
            cc.log('塔已经是最高级');
            return;
        }

        let upgrade_chip: number = this.tower_params[this.tower_type][tower_level].build_chip
            - this.tower_params[this.tower_type][tower_level - 1].build_chip;

        // 金币不够
        if (upgrade_chip > UG.get_uchip()) {
            cc.log('升级塔金币不够~');
            return;
        }

        // 升级塔
        tower_level = tower_com.upgrade_tower();

        // 消耗金币
        UG.add_chip(-upgrade_chip);
        this.game_scene.show_game_uchip();
    }

    // 塔移除
    private _remove_builded_tower(): void {
        // 判断是否有塔
        if (!this.is_builded) {
            return;
        }
        // 移除塔
        this.builded_tower.destroy();
        this.is_builded = false;
        this.tower_type = 0;
        this.tower_sprite_node.active = true;
    }

    // 塔卖掉
    public on_tower_sell_click(): void {
        // 回收金币
        let tower_com: any = this.get_tower_com();
        let tower_level: any = tower_com.get_tower_level();
        let sell_chip: number = this.tower_params[this.tower_type][tower_level - 1].build_chip;
        UG.add_chip(sell_chip);
        this.game_scene.show_game_uchip();

        // 移除塔
        this._remove_builded_tower();
    }
}
